$(function () {

    // 如果是登录状态，首页导航栏小人图标中的“立即登陆”变为“我的订单”
    if (location.href.indexOf("#") == -1) {
        //在当前页面地址加入"#",只刷新一次页面
        location.href = location.href + "#";
        for (var i = 0; i < 2; i++) {
            location.reload();
        }
    }

    if (document.cookie) {
        var arr = document.cookie.split('; ');
        arr.forEach(function (elm, i) {
            var item = elm.split('=');
            if (item[0] == "loginStatus") {
                if (item[1]) {
                    $(".login").removeClass("login").addClass("myStore").html("个人中心");
                    $(".myStore").on("click", function () {
                        location.href = "../html/myerror.html";
                    })
                }
            }
        });
    }

    var phoneBigPic = $(".phoneDe>ul");
    var voicePic = $(".voiceDe>ul");
    var fixings = $(".fixings>ul");
    var life = $(".life>ul");
    var community = $(".community>ul");
    // var login = $(".login");
    // var register = $(".register");
    var accessBox = $(".access-box>p");
    var count = 1;

    $.ajax({
        type: "get",
        url: "../lib/index1.php",
        dataType: "json",
        success: function (response) {
            var template = '';
            var template1 = '';
            var count = 1;
            response.forEach((elm, i) => {
                function insert_voice_fixing_life() {
                    if (count > 8) {
                        count = 1;
                    }
                    var pic = JSON.parse(elm.pic);
                    // 如果是第1条或第五条数据
                    if (count == 1 || count == 5) {
                        // 如果该商品有原始价格
                        if (elm.old_price) {
                            template1 += `<li class="small-pic">
                        <a href="../html/product_detail.html?id=${elm.id}">
                            <img class="lazy" data-original="..${pic.src}" alt="${pic.title}">
                            <div>
                                <p>${elm.title}</p>
                                <p>${elm.details}</p>
                                <span>
                                    <dfn>￥</dfn>
                                    ${elm.price}
                                    <s>${elm.old_price}</s>
                                </span>
                            </div>
                        </a>
                    </li>`;
                            count++;
                        } else {
                            // 如果没有原始价格
                            template1 += `<li class="small-pic">
                        <a href="../html/product_detail.html?id=${elm.id}">                        
                            <img class="lazy" data-original="..${pic.src}" alt="${pic.title}">
                            <div>
                                <p>${elm.title}</p>
                                <p>${elm.details}</p>
                                <span>
                                    <dfn>￥</dfn>
                                    ${elm.price}
                                </span>
                            </div>
                        </a>
                    </li>`;
                            count++;
                        }
                    } else {
                        if (elm.old_price) {
                            template1 += `<li class="small-pic">
                        <a href="../html/product_detail.html?id=${elm.id}">                       
                            <img class="lazy" data-original="..${pic.src}" alt="${pic.title}">
                            <p></p>
                            <p>${elm.title}</p>
                            <p>${elm.details}</p>
                            <span>
                                <dfn>￥</dfn>
                                ${elm.price}
                            <s>${elm.old_price}</s>
                        </a>
                    </li>`;
                            count++;
                        } else {
                            template1 += `<li class="small-pic">
                        <a href="../html/product_detail.html?id=${elm.id}">                        
                            <img class="lazy" data-original="..${pic.src}" alt="${pic.title}">
                                <p></p>
                                <p>${elm.title}</p>
                                <p>${elm.details}</p>
                                <span>
                                    <dfn>￥</dfn>
                                    ${elm.price}
                                </span>
                            </a>
                        </li>`;
                            count++;
                        }
                    }
                    return template1;
                }
                // 如果商品类型是手机
                if (elm.type == "phone") {
                    var pic = JSON.parse(elm.pic);
                    // console.log(pic.src);
                    // console.log(i);
                    // 如果是手机大图
                    if (i <= 1) {
                        template += `<li class="big-pic">
                        <a href="../html/product_detail.html?id=${elm.id}">                  
                            <p class="icon">新品</p>
                            <p>${elm.title}</p>
                            <p>${elm.details}</p>
                            <span>
                                <dfn>￥</dfn>
                                ${elm.price}
                            </span>
                            <img class="lazy" data-original="..${pic.src}" alt="..${pic.src}">
                        </a>
                    </li>`;
                    } else {
                        template += `  <li class="small-pic">
                        <a href="../html/product_detail.html?id=${elm.id}">                     
                            <img class="lazy" data-original="..${pic.src}" alt="..${pic.src}">
                            <p class="icon">领券</p>
                            <p>${elm.title}</p>
                            <p>${elm.details}</p>
                            <span>
                                <dfn>￥</dfn>
                                ${elm.price}
                            </span>
                        </a>
                    </li>`;
                    }
                } else if (elm.type == "earphone") {
                    // 如果商品类型是耳机
                    insert_voice_fixing_life();
                    voicePic.append(template1);
                    template1 = "";
                } else if (elm.type == "fixings") {
                    // 如果商品类型是配件
                    insert_voice_fixing_life();
                    fixings.append(template1);
                    template1 = "";
                } else if (elm.type == "life_around") {
                    insert_voice_fixing_life();
                    life.append(template1);
                    template1 = "";
                }
            })
            phoneBigPic.append(template);
        }
    });
    // 社区热帖部分的Ajax
    $.ajax({
        type: "get",
        url: "../lib/index1_comment.php",
        dataType: "json",
        success: function (response) {
            var template = '';
            var count = 1;
            response.forEach((elm, i) => {
                var bigimage = JSON.parse(elm.bigimage);
                var userIcon = JSON.parse(elm.user_icon);
                template += ` <li class="small-pic">
                        <a href="#">
                            <img class="lazy" data-original="..${bigimage.src}" alt="${bigimage.title}">
                            <p></p>
                            <p>
                                <img class="lazy" data-original="..${userIcon.src}" alt="${userIcon.title}">
                                ${elm.username}
                            </p>
                            <p class="comment-p">${elm.comment}</p>
                            <p class="user-phone-type">${elm.classify}</p>
                        </a>
                    </li>`;
            })
            community.append(template);
        }
    });
    // 头部的小人功能
    accessBox.on("click", function () {
        if ($(this).hasClass("login")) {
            location.href = "../html/login.html";
        } else if ($(this).hasClass("register")) {
            location.href = "../html/register.html";
        } else {
            location.href = "../html/myerror.html";
        }
    })
    $(document).one("mouseenter", "img", function () {
        $("img.lazy").lazyload({
            placeholder: "../img/loading.png",
            effect: "fadeIn"
        });
    })
})